package com.hymall.controller.wx;

import com.hymall.bean.MarketUser;
import com.hymall.bean.common.BasePageInfo;
import com.hymall.bean.common.BaseRespVo;
import com.hymall.bean.common.CommonData;
import com.hymall.bean.vo.WxCouponVo;
import com.hymall.service.wx.WxCouponService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author: 叶熠彬
 * @description: 用户端优惠券相关业务
 * @date: 2022/7/19 11:02
 */
@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {

    @Autowired
    WxCouponService wxCouponService;

    /**
     * 根据state查询优惠券
     * 每次查询判断时间，更新状态码
     *
     * @author: 叶熠彬
     * @date: 2022/7/19 11:26
     * @param: [status, info]
     * @return: com.hymall.bean.common.BaseRespVo
     */
    @GetMapping("mylist")
    public BaseRespVo userCouponList(int status, BasePageInfo info) {
        Subject subject = SecurityUtils.getSubject();
        Integer userId;
        try {
            MarketUser principal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
            userId = principal.getId();
        } catch (Exception e) {
            return BaseRespVo.error(501, "请登录");
        }
        CommonData date = wxCouponService.queryCouponList(userId, status, info);
        return BaseRespVo.ok(date);
    }

    /**
     * 兑换优惠券
     * TODO 根据不同用户
     *
     * @author: 叶熠彬
     * @date: 2022/7/19 19:31
     * @param: [map]
     * @return: com.hymall.bean.common.BaseRespVo
     */
    @PostMapping("exchange")
    public BaseRespVo exchangeCoupon(@RequestBody Map map) {
        String code = (String) map.get("code");
        int result = wxCouponService.addCouponByCode(code);
        if (result == 742) {
            return BaseRespVo.error(742, "优惠券不存在或库存不足");
        }
        if (result == 740) {
            return BaseRespVo.error(740, "你已经领取过该优惠券了");
        }
        if (result == 200) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.error(743, "领取失败，请稍后重试");
    }

    /**
     * 显示购物车选择优惠券列表
     *
     * @author: 叶熠彬
     * @date: 2022/7/19 21:52
     * @param: [map]
     * @return: com.hymall.bean.common.BaseRespVo
     */
    @GetMapping("selectlist")
    public BaseRespVo cartCouponList(Integer cartId,Integer grouponRulesId) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser principal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        if (principal==null){
            return BaseRespVo.error(501, "请登录");
        }
        Integer userId = principal.getId();
        //加上了available
        CommonData data = wxCouponService.queryCartAvailableCoupon(userId);
        return BaseRespVo.ok(data);
    }

    /**
     * 优惠券专区
     *
     * @author: 叶熠彬
     * @date: 2022/7/19 22:15
     * @param: [map]
     * @return: com.hymall.bean.common.BaseRespVo
     */
    @GetMapping("list")
    public BaseRespVo couponList(BasePageInfo info) {
        CommonData<WxCouponVo> data = wxCouponService.queryWxAvailableCoupon(info);
        return BaseRespVo.ok(data);
    }

    /**
     * 优惠券专区领取优惠券
     *
     * @author: 叶熠彬
     * @date: 2022/7/19 23:23
     * @param: [info]
     * @return: com.hymall.bean.common.BaseRespVo
     */
    @PostMapping("receive")
    public BaseRespVo receiveCoupon(@RequestBody Map map) {
        Integer couponId = (Integer) map.get("couponId");
        Subject subject = SecurityUtils.getSubject();
        try {
            MarketUser principal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.error(501, "请先登录");
        }
        //TODO 查询状态，写入UserCoupon
        int code = wxCouponService.receiveCoupon(couponId);
        if (code == 0) {
            return BaseRespVo.ok();
        }
        if (code == 740) {
            return BaseRespVo.error(740, "你已经领取过该优惠券了");
        }
        if (code == 742) {
            return BaseRespVo.error(742, "优惠券不存在或库存不足");
        }
        return BaseRespVo.error(743, "领取失败，请稍后重试");
    }

}
